21
Build Your Own Game—Tic Tac Toe
21
Changing the properties of any control is easy. Simply click on the control, and
the “Properties Window” on the left will show you the current set of properties and
allow you to change them. You can greatly accelerate your work by using the copy
(Ctrl-C) and Paste (Ctrl-V) functions. Once you have created TextBox1, simply copy
and paste eight times. VBA will automatically assign the field names the numbers 2
to 9 as a suffix to keep them unique.
You do have to position them in the right place as shown in the picture, since the
“Paste” function puts the text box in an odd spot on the canvas.
There are five more TextBoxes to add:
• TextBox10: Message bar (Default: Click Play to Start”)
• TextBox11: Win score of Computer (Default 0)
• TextBox12: Win score for Human User (Default 0)
• TextBox13: Draw score for Computer (Default 0)
• TextBox14: Draw score for Human User (Default 0)
Add the default values in the “Value” property of each textbox.
Now, let’s work on the remaining four controls.
The Labels are the easiest, since they do not have any function other than to dis
play a name for the different parts of your canvas. There are five labels, as listed
below. You should be able to create and place them in the correct positions without
much difficulty. The names of the label fields are not important, since we are rarely
going to refer to them in our program. But the displayed text must be entered in the
“Caption” property.
• TIC TAC TOE
• I Play First
• U Play First
• Win (twice)
• Draw (twice)
The CommandButtons (Play and Exit) have code associated with them, which I will
cover later. Just for the canvas, treat them like labels. Get the Caption filled in and
color them as you please, using the “BackColor” property.
The OptionButtons allow you to select one of many options. There are only two in this
canvas, under each label representing the two options of “I Play First” and “U Play First”.
There are two important properties of OptionButtons that you must know. The Value is
a default that appears when you launch the form. Make sure OptionButton2 (“U Play
First”) has a value of true and OptionButton1 (I Play First) is False to begin the game
with a default of “U Play first”. The second property is the GroupName. Make sure the
GroupName is set to the same value for both the OptionButtons. Yes, they can be blank.
Who is “I” or “U” in this game? Well, the idea here is that the “Computer” is pre
senting the game, hence Computer is “I” and the human player is “U”. This is why
the two images are assigned. You can attach any image you like for these two Image
controls—they are only for show (no code association).
Now that we have built the form, let us breathe life into it.